﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace TimKen.BussLogic
{
    /// <summary>
    ///  站内消息操作类
    /// </summary>
    public class InboxManager
    {
        private readonly TimKen.DataAccess.InboxDAO dal = new TimKen.DataAccess.InboxDAO();
        public InboxManager()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int MsgID)
        {
            return dal.Exists(MsgID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>

        public int Add(TimKen.DataEntity.Inbox model)
        {
            model.MsgNo = Guid.NewGuid().ToString();
            model.CreateDate = DateTime.Now;

            if (model.ToAll==1)
            {
                 dal.AddAll(model);
                 return 0;
            }
            else
            {
                string userID = model.ToID;
                IList<string> userIDs = userID.Split(',');
                if (userIDs.Count > 1) {
                    model.ToAll = 2;
                }
                int MsgID = 0;
                foreach (string toID in userIDs)
                {
                    if (toID != "")
                    {
                        model.ToID = toID;
                        MsgID = dal.Add(model);
                    }
                }
                return MsgID;
            }

        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.Inbox model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="MsgNo"></param>
        /// <param name="DeleteType">1为从发件箱删除，0为从收件箱删除</param>
        public void Delete(string MsgNo, int DeleteType)
        {

            dal.Delete(MsgNo, DeleteType);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int MsgID)
        {

            dal.Delete(MsgID);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.Inbox GetModel(int MsgID)
        {

            return dal.GetModel(MsgID);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据总数
        /// </summary>
        public DataSet GetCount(string strWhere)
        {
            return dal.GetCount(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string ToID, string FromID, int IsRead)
        {
            return dal.GetList(PageSize, PageIndex, ToID, FromID, IsRead);
        }

        #endregion  成员方法
    }
}

